package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingListDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSettingList;
import com.itheima.service.OrderSettingListService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

@Service
@Transactional
public class OrderSettingListServiceImpl implements OrderSettingListService {

    @Autowired
    private OrderSettingListDao orderSettingListDao;
    @Autowired
    private OrderDao orderDao;
    @Override
    public void add(Member member, Integer[] setmealIds) {
        //调用dao，将基本信息存储进去
        //没有id，需要反向获取
        String phoneNumber = member.getPhoneNumber();
        //通过电话预约的手机号查询是否为会员
        Member member1 = orderSettingListDao.findByphone(phoneNumber);
        if(member1 == null){
            orderSettingListDao.add(member);
        }
        //添加成功了，反向获取数据库自增的id
        Integer memberId = member1.getId();   //预约的id
        //对前端的检查项数据进行非空判断
        if (setmealIds != null && setmealIds.length > 0) {
            //表示前端传过来的有数据
            Map<String, Integer> map = new HashMap<>();
            for (Integer setmealId : setmealIds) {
                map.put("setmealId", setmealId);
                //map.put("memberId", memberId);
                //预约成功了。需要在预约信息表中添加上本次预约的信息
                Order order = new Order();
                order.setMemberId(memberId);
                order.setOrderDate(member.getRegTime());
                order.setOrderType(Order.ORDERTYPE_TELEPHONE);
                order.setOrderStatus(Order.ORDERSTATUS_NO);
                order.setSetmealId(map.get("setmealId"));//预约套餐的id
                //将预约信息添加到预约表里面
                orderDao.add(order);
            }
        }
    }
    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        //调用dao  进行模糊查询
        Page<OrderSettingList>page = orderSettingListDao.findByCondition(queryString);
        long total = page.getTotal();   //总条数
        List<OrderSettingList> result = page.getResult();//所有的数集合
        return new PageResult(total,result);

    }
    /**
     * 修改预约状态
     * */
    public void update(Integer id , String orderDate, Integer setmealId) {
        orderSettingListDao.update(String.valueOf(id),orderDate,String.valueOf(setmealId));

    }
    /**
     *  取消预约，并删除信息
     * @param id
     * @param orderDate
     * @param setmealId
     */
    public void delete(Integer id, String orderDate, Integer setmealId) {
        String s = orderSettingListDao.getorderStatus(String.valueOf(id),orderDate,String.valueOf(setmealId));
        if(s.equals("已到诊")){
            throw new RuntimeException();
        }
        orderSettingListDao.delete(String.valueOf(id),orderDate,String.valueOf(setmealId));
    }
}
